package sipus.common;

import java.io.Serializable;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.metadata.ClassMetadata;
import org.zkoss.zk.ui.sys.SessionsCtrl;

import sipus.database.hibernate.HibernateUtil;
import sipus.database.model.DetailLogLogin;
import sipus.database.model.LogUserActifity;
import sipus.database.model.Menu;
import sipus.database.model.RolePrivilage;
import sipus.database.model.Tbmrole;
import sipus.database.model.Tbmuser;


public class CommonPrivilages {

	public final static Integer READ = 0;
	public final static Integer CREATE = 1;
	public final static Integer UPDATE = 2;
	public final static Integer DELETE = 3;
	
	
	@SuppressWarnings("rawtypes")
	public static boolean saveActivity(Class classCalled, Integer activityType,
			Serializable serializable, String perubahan) {
		try {
			DetailLogLogin detailLogLogin = (DetailLogLogin) (SessionsCtrl
					.getCurrent() == null ? null : SessionsCtrl.getCurrent()
					.getAttribute("detailLogLogin"));
			if (detailLogLogin != null) {
				String activity = activityType.equals(READ) ? "Membaca data "
						+ serializable.getClass().getSimpleName()
						: activityType.equals(CREATE) ? "Menyimpan data baru "
								+ serializable.getClass().getSimpleName()
								: activityType.equals(UPDATE) ? "Merubah data "
										+ serializable.getClass()
												.getSimpleName()

										+ classCalled.getSimpleName()
										: activityType.equals(DELETE) ? "Menghapus data "
												+ serializable.getClass()
														.getSimpleName()
												: "";
				String keterangan1 = "";
				if (serializable != null) {
					ClassMetadata classMetadata = HibernateUtil
							.getClassMetadata(serializable.getClass());
					Serializable id = classMetadata.getIdentifier(serializable,
							EntityMode.POJO);
					keterangan1 += "id=" + id + "\n";
					String[] properties = classMetadata.getPropertyNames();
					for (String property : properties) {
						Object value = classMetadata.getPropertyValue(
								serializable, property, EntityMode.POJO);
						Serializable mySerializable = null;
						try {
							ClassMetadata myClassMetadata = HibernateUtil
									.getClassMetadata(value.getClass());
							mySerializable = (Serializable) myClassMetadata
									.getPropertyValue(value, "nama",
											EntityMode.POJO);
						} catch (Exception e) {

						}
						if (value == null)
							continue;
						keterangan1 += ""
								+ property
								+ "="
								+ (mySerializable == null ? value
										: mySerializable) + "\n";
					}
				}
				if (keterangan1.length() < 255) {
					keterangan1 += "";
				} else {
					keterangan1 = keterangan1.substring(0, 253);
				}
				if (perubahan.length() < 255) {
					perubahan += "";
				} else {
					perubahan = perubahan.substring(0, 253);
				}

				LogUserActifity actifity = new LogUserActifity();
				actifity.setDetailLogLogin(detailLogLogin);
				actifity.setKeterangan(activity);
				actifity.setKeterangan1(keterangan1);
				actifity.setKeterangan12(perubahan);
				actifity.setActivityType(activityType);
				actifity.setClassCalled(classCalled.getName());
				Session session = HibernateUtil.currentSession();
				session.save(actifity);

			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	

	public static boolean checkPrevilages(Integer privilagesCode) {
		boolean result = false;
		Menu menu = Common.getCurrentMenu();
		System.out.println("Current menu = " + menu + ", privilagesCode = "
				+ privilagesCode);
		if (menu == null) {
			return result;
		}
		Tbmuser tbmuser = Common.getCurrentUser();
		Tbmrole tbmrole = tbmuser.getUserRole();

		Session session = HibernateUtil.currentSession()
				;

		RolePrivilage rolePrivilage = (RolePrivilage) session.createCriteria(
				RolePrivilage.class).add(Restrictions.eq("role", tbmrole)).add(
				Restrictions.eq("menu", menu)).setMaxResults(1).uniqueResult();

		System.out.println("rolePrivilage = " + rolePrivilage + ", role = "
				+ tbmrole);

		if (rolePrivilage != null) {
			System.out.println("READ = " + rolePrivilage.getRead()
					+ ", CREATE = " + rolePrivilage.getCreate() + ", UPDATE = "
					+ rolePrivilage.getUpdate() + ", DELETE = "
					+ rolePrivilage.getDelete());
			if (READ.equals(privilagesCode)) {
				result = rolePrivilage.getRead().equals(1);
			} else if (CREATE.equals(privilagesCode)) {
				result = rolePrivilage.getCreate().equals(1);
			} else if (UPDATE.equals(privilagesCode)) {
				result = rolePrivilage.getUpdate().equals(1);
			} else if (DELETE.equals(privilagesCode)) {
				result = rolePrivilage.getDelete().equals(1);
			}
		}

		

		return result;
	}

}
